package com.yuexiang.melodyhub.authserver.handler;

import com.yuexiang.melodyhub.common.constant.UsualConstant.BizCodeConstant;
import com.yuexiang.melodyhub.common.exception.RequestArgumentException;
import com.yuexiang.melodyhub.common.exception.UnAccessDeniedException;
import com.yuexiang.melodyhub.common.exception.gateway.UnavailableServiceException;
import com.yuexiang.melodyhub.common.result.ResultData;
import com.yuexiang.melodyhub.common.result.ReturnCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(UnavailableServiceException.class)
    public ResponseEntity<ResultData<?>> handleUnavailableService(UnavailableServiceException ex) {
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(ResultData.fail(ReturnCode.FEIGN_SERVER_NOT_USE.getCode(), ReturnCode.FEIGN_SERVER_NOT_USE.getMsg()));
    }

    @ExceptionHandler(UnAccessDeniedException.class)
    public ResponseEntity<ResultData<?>> handleUnAccessDeniedService(UnAccessDeniedException ex) {
        return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(ResultData.fail(ReturnCode.FORBIDDEN.getCode(), ReturnCode.FORBIDDEN.getMsg()));
    }

    @ExceptionHandler(RequestArgumentException.class)
    public ResponseEntity<ResultData<?>> handleBizCusService(RequestArgumentException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ResultData.fail(BizCodeConstant.Code.REQUEST_ARGUMENT_NOT_VALID_CODE, ex.getMessage()));
    }

}
